﻿Public Class ucMyCombo
    Inherits Windows.Forms.ComboBox

    Private dicItems As New Dictionary(Of String, Integer)
    Private dicItemsValue As New Dictionary(Of Integer, String)

    Private Sub addItem(ByVal value As Integer, ByVal display As String)

        If Not dicItems.ContainsKey(display) Then
            dicItems.Add(display, value)
            dicItemsValue.Add(value, display)

            Me.Items.Add(display)
        End If

    End Sub

    Public Function getSelectedID() As Integer
        If Not Me.SelectedItem = Nothing Then
            If dicItems.ContainsKey(Me.SelectedItem) Then
                Return dicItems.Item(Me.SelectedItem)
            End If

        Else
            '** in some cases the selectedItem is empty but the combobox contains a value
            If dicItems.ContainsKey(Text.ToString) Then
                Return dicItems.Item(Text.ToString)
            End If

        End If



        Return -1
    End Function

    Public WriteOnly Property SetID As Integer
        Set(value As Integer)

            If dicItemsValue.ContainsKey(value) Then

                Me.SelectedItem = dicItemsValue(value)
                'Me.SelectedText = dicItemsValue(value)
                'Me.SelectedItem = value
            End If

        End Set
    End Property

    '** Provide a sql string in such a manner that the first column consists of the
    '** id's and the second with the names

    Public Sub fillData(ByVal stSQL As String, Optional ByVal addAll As Boolean = False)
        Dim cn As New SQLite.SQLiteConnection ' ADODB.Connection()
        Dim da As New SQLite.SQLiteDataAdapter 'Dim rs As New ADODB.Recordset()
        Dim ds As New DataSet

        Try
            '** first clear all data
            dicItems.Clear()
            dicItemsValue.Clear()
            Me.Items.Clear()

            If addAll Then
                addItem(0, "<All>")
            End If

            '** Open connection
            cn.ConnectionString = fGetConnectionString()
            cn.Open()

            '** set up de recordset
            'rs.CursorLocation = ADODB.CursorLocationEnum.adUseClient
            'rs.CursorType = ADODB.CursorTypeEnum.adOpenDynamic
            'rs.LockType = ADODB.LockTypeEnum.adLockOptimistic

            'rs.Open(stSQL, cn)
            da = New SQLite.SQLiteDataAdapter(stSQL, cn)
            da.Fill(ds, "COMBO")

            'If rs.RecordCount > 0 Then
            If ds.Tables(0).Rows.Count > 0 Then
                'Do While Not rs.EOF
                For i = 1 To ds.Tables(0).Rows.Count - 1
                    'addItem(rs.Fields(0).Value, rs.Fields(1).Value)
                    addItem(ds.Tables(0).Rows(i)(0), ds.Tables(0).Rows(i)(1).ToString)

                Next i
                'Loop
            End If

            '** clean up
            'rs.Close()
            'rs = Nothing

            cn.Close()




        Catch ex As Exception
            mdlLOGGING.fLOG("ucMyCombo => fillData: " & ex.Message.ToString, 99)
        End Try

        cn = Nothing

    End Sub


    Public Sub AutoCompleteCombo_KeyUp(ByVal cbo As ComboBox, ByVal e As KeyEventArgs)
        '*// http://www.codeproject.com/KB/cpp/autocomplete_combobox.aspx
        Dim sTypedText As String
        Dim iFoundIndex As Integer
        Dim oFoundItem As Object
        Dim sFoundText As String
        Dim sAppendText As String

        'Allow select keys without Autocompleting
        Select Case e.KeyCode
            Case Keys.Back, Keys.Left, Keys.Right, Keys.Up, Keys.Delete, Keys.Down
                Return
        End Select

        'Get the Typed Text and Find it in the list
        sTypedText = cbo.Text
        iFoundIndex = cbo.FindString(sTypedText)

        'If we found the Typed Text in the list then Autocomplete
        If iFoundIndex >= 0 Then

            'Get the Item from the list (Return Type depends if Datasource was bound 
            ' or List Created)
            oFoundItem = cbo.Items(iFoundIndex)

            'Use the ListControl.GetItemText to resolve the Name in case the Combo 
            ' was Data bound
            sFoundText = cbo.GetItemText(oFoundItem)

            'Append then found text to the typed text to preserve case
            sAppendText = sFoundText.Substring(sTypedText.Length)
            cbo.Text = sTypedText & sAppendText

            'Select the Appended Text
            cbo.SelectionStart = sTypedText.Length
            cbo.SelectionLength = sAppendText.Length

        End If

    End Sub


    Public Sub AutoCompleteCombo_Leave(ByVal cbo As ComboBox)
        Dim iFoundIndex As Integer

        iFoundIndex = cbo.FindStringExact(cbo.Text)

        cbo.SelectedIndex = iFoundIndex

    End Sub


    Private Sub ucMyCombo_KeyUp(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        AutoCompleteCombo_KeyUp(Me, e)
    End Sub

    Private Sub ucMyCombo_Leave(sender As Object, e As System.EventArgs) Handles Me.Leave
        AutoCompleteCombo_Leave(Me)
    End Sub
End Class
